#include <iostream>
#include <boost/asio.hpp>

const int max_length = 1024;

int main(void)
{
    try
    {
        boost::asio::io_context io_context;
        boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
        boost::asio::ip::tcp::endpoint endpoint(address, 12345);

        boost::asio::ip::tcp::socket socket(io_context);
        boost::system::error_code error = boost::asio::error::host_not_found;
        socket.connect(endpoint, error);
        if (error)
        {
            std::cerr << "Connect failed: " << error.message() << "\n";
            return -1;
        }

        std::cout << "Enter message: ";
        char request[max_length];
        std::cin.getline(request, max_length);
        size_t length = std::strlen(request);
        boost::asio::write(socket, boost::asio::buffer(request, length));

        char reply[max_length];
        size_t reply_length = boost::asio::read(socket,
            boost::asio::buffer(reply, length));
        std::cout << "Reply is: ";
        std::cout.write(reply, reply_length);
        std::cout << "\n";
    }
    catch (const std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
        return -2;
    }

    return 0;
}